# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):
    def create_corp(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(12),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        return res['data']

    def test_corp_user_list(self):
        corp = self.create_corp()
        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)

    def test_corp_user_list_status(self):
        corp = self.create_corp()
        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)
        self.assertEqual(res['data']['itemList'][0]['user_obj']['id'], user_id)

        res = self.c.open('/api/corp-user/delete', {'corp': corp['id'], 'user': user_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'normal'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'delete'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)


    def test_corp_user_revert(self):
        corp = self.create_corp()
        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)
        self.assertEqual(res['data']['itemList'][0]['user_obj']['id'], user_id)

        res = self.c.open('/api/corp-user/delete', {'corp': corp['id'], 'user': user_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'normal'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'delete'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)

        res = self.c.open('/api/corp-user/revert', {'corp': corp['id'], 'user': user_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'normal'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'delete'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 0)



    def test_corp_user_create(self):
        corp = self.create_corp()
        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)
        self.assertEqual(res['data']['itemList'][0]['user_obj']['id'], user_id)

    def test_corp_user_create_login(self):
        corp = self.create_corp()
        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

    def test_corp_user_delete(self):
        corp = self.create_corp()
        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)

        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1'})
        self.assertEqual(res['data']['itemList'][0]['user_obj']['id'], user_id)

        res = self.c.open('/api/corp-user/delete', {'corp': corp['id'], 'user': user_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'normal'})
        self.assertNotEqual(res['data']['itemList'][0]['user_obj']['id'], user_id)

    def test_corp_user_delete_by_id(self):
        corp = self.create_corp()
        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)

        user_id = res['data']['user_obj']['id']
        id = res['data']['id']

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1'})
        self.assertEqual(res['data']['itemList'][0]['user_obj']['id'], user_id)

        res = self.c.open('/api/corp-user/delete', {'corp': corp['id'], 'id': id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-user/list', {'corp': corp['id'], 'orderBy': 'corp_user.id', 'orderDesc': '1', 'status': 'normal'})
        self.assertNotEqual(res['data']['itemList'][0]['user_obj']['id'], user_id)


    def test_corp_user_option(self):
        corp = self.create_corp()
        res = self.c.open('/api/corp-user/option', {'corp': corp['id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)

        p = {
            'corp': corp['id'],
            'username': self.randchar(12), 
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp-user/create', p)
        self.assertEqual(res['code'], 0)

        id = res['data']['id']

        res = self.c.open('/api/corp-user/option', {'corp': corp['id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)
        self.assertEqual(res['data']['itemList'][0]['value'], id)


test_list = [
    'test_corp_user_list',
    'test_corp_user_create',
    'test_corp_user_create_login',
    'test_corp_user_delete',
    'test_corp_user_delete_by_id',
    'test_corp_user_option',
    'test_corp_user_list_status',
    'test_corp_user_revert',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



